package com.hnkywl.sts.entity.jggl;

import com.hnkywl.sts.entity.BaseEntity;
import com.hnkywl.sts.entity.organ.Gsjg;
import com.hnkywl.sts.entity.organ.Qyjg;
import com.hnkywl.sts.entity.sys.Yg;
import org.hibernate.annotations.*;

import javax.persistence.*;
import javax.persistence.Entity;
import javax.persistence.Table;
import java.util.Date;
import java.util.List;

/**
 * Created by Melody on 14-3-11.
 */
@Entity
@Table(name = "T_BIZ_JGD")
@Where(clause = "deleted!='1'")
public class Jgd extends BaseEntity {
    @Id
    @SequenceGenerator(name = "seqJgd", allocationSize = 1, sequenceName = "SEQ_BIZ_JGD")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seqJgd")
    private Long id;

    /**
     * 加工单号
     */
    private String jgdh;

    /**
     * 加工日期
     */
    private Date jgrq;

    /**
     * 申请公司
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "SQGS_ID")
    @NotFound(action = NotFoundAction.IGNORE)
    private Qyjg sqgs;

    /**
     * 申报公司
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "SBGS_ID")
    @NotFound(action = NotFoundAction.IGNORE)
    private Gsjg sbgs;

    /**
     * 申报人
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "SBR_ID")
    @NotFound(action = NotFoundAction.IGNORE)
    private Yg sbr;

    /**
     * 申报日期
     */
    private Date sbrq;

    /**
     * 状态：已保存0，已提交1，已完结 2
     */
    private Integer zt;

    /**
     * 已出库
     */
    private Boolean yck = false;

    /**
     * 已入库
     */
    private Boolean yrk = false;

    /**
     * 备注
     */
    private String bz;

    /**
     * 预期成材料率
     */
    private String rate;
    /**
     * 实际成材率
     */
    @Formula("ROUND(((select sum(decode(t.JG_SBZL,0,0,null,0,t.JG_SBZL)) from T_BIZ_JGDMX t where t.JGD_ID=ID and t.DELETED!=1 and t.ZT=1)/nvl((select sum(decode(t.JG_SBZL,0,1,null,1,t.JG_SBZL)) from T_BIZ_JGDMX t where t.JGD_ID=ID and t.DELETED!=1 and t.ZT=0),1)),4)*100")
    private String sjrate;

    /**
     * 加工单明细（加工前）
     */
    @OneToMany(fetch = FetchType.LAZY, targetEntity = Jgdmx.class)
    @Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE})
    @JoinColumn(name = "JGD_ID")
    private List<Jgdmx> jgdmxList;

    /**
     * 加工单明细（加工后）
     */
    @OneToMany(fetch = FetchType.LAZY, targetEntity = Jghjgdmx.class)
    @Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE})
    @JoinColumn(name = "JGD_ID")
    private List<Jghjgdmx> jhgjgdmxList;

    /**
     * 申报数量和
     */
    @Formula("(select sum(t.SBZL) from T_BIZ_JGDMX t where t.JGD_ID=ID and t.DELETED!=1 and t.ZT=0)")
    private Double sumsbzl;

    /**
     * 加工前重量
     */
    @Formula("(select sum(t.JG_SBZL) from T_BIZ_JGDMX t where t.JGD_ID=ID and t.DELETED!=1 and t.ZT=0)")
    private Double jgzl;

    /**
     * 加工后重量
     */
    @Formula("(select sum(t.JG_SBZL) from T_BIZ_JGDMX t where t.JGD_ID=ID and t.DELETED!=1 and t.ZT=1)")
    private Double jghzl;

    public String getSjrate() {
        return sjrate;
    }

    public void setSjrate(String sjrate) {
        this.sjrate = sjrate;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public List<Jgdmx> getJgdmxList() {
        return jgdmxList;
    }

    public void setJgdmxList(List<Jgdmx> jgdmxList) {
        this.jgdmxList = jgdmxList;
    }

    public String getJgdh() {
        return jgdh;
    }

    public void setJgdh(String jgdh) {
        this.jgdh = jgdh;
    }

    public Date getJgrq() {
        return jgrq;
    }

    public void setJgrq(Date jgrq) {
        this.jgrq = jgrq;
    }

    public Qyjg getSqgs() {
        return sqgs;
    }

    public void setSqgs(Qyjg sqgs) {
        this.sqgs = sqgs;
    }

    public Gsjg getSbgs() {
        return sbgs;
    }

    public void setSbgs(Gsjg sbgs) {
        this.sbgs = sbgs;
    }

    public Yg getSbr() {
        return sbr;
    }

    public String getBz() {
        return bz;
    }

    public void setBz(String bz) {
        this.bz = bz;
    }

    public void setSbr(Yg sbr) {
        this.sbr = sbr;
    }

    public Date getSbrq() {
        return sbrq;
    }

    public void setSbrq(Date sbrq) {
        this.sbrq = sbrq;
    }

    public Integer getZt() {
        return zt;
    }

    public void setZt(Integer zt) {
        this.zt = zt;
    }

    public Boolean getYck() {
        return yck;
    }

    public void setYck(Boolean yck) {
        this.yck = yck;
    }

    public Boolean getYrk() {
        return yrk;
    }

    public void setYrk(Boolean yrk) {
        this.yrk = yrk;
    }

    public List<Jghjgdmx> getJhgjgdmxList() {
        return jhgjgdmxList;
    }

    public void setJhgjgdmxList(List<Jghjgdmx> jhgjgdmxList) {
        this.jhgjgdmxList = jhgjgdmxList;
    }

    public Double getJgzl() {
        return jgzl;
    }

    public void setJgzl(Double jgzl) {
        this.jgzl = jgzl;
    }

    public Double getJghzl() {
        return jghzl;
    }

    public void setJghzl(Double jghzl) {
        this.jghzl = jghzl;
    }

    public Double getSumsbzl() {
        return sumsbzl;
    }

    public void setSumsbzl(Double sumsbzl) {
        this.sumsbzl = sumsbzl;
    }

    public String getRate() {
        return rate;
    }

    public void setRate(String rate) {
        this.rate = rate;
    }
}
